package io.github.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import io.github.annotation.Log;
import io.github.entity.SysDepartEntity;
import io.github.service.SysDepartService;
import io.github.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: Max
 * @Date: 2019/10/9 9:52
 */

@RestController
@RequestMapping("/admin/sys/depart")
public class SysDepartController extends AbstractController{

    @Autowired
    SysDepartService departService;

    @RequestMapping("/list")
    public R list(@RequestBody JSONObject jsonObject){
        List<SysDepartEntity> departList = departService.listAll();
        return R.ok().put("data",departList);
    }

    @RequestMapping("/listOrg")
    public R listOrg(){
        List<SysDepartEntity> departList = departService.listOrg();
        return R.ok().put("data",departList);
    }

    @RequestMapping("/listDep")
    public R listDep(@RequestBody JSONObject jsonObject){
        Integer orgId = jsonObject.getInteger("orgId");
        String depName = jsonObject.getString("depName");
        List<SysDepartEntity> departList = departService.listDep(orgId,depName);
        return R.ok().put("data",departList);
    }

    @Log(value = "单位添加",action = 1)
    @RequestMapping("/add")
    public R save(@RequestBody SysDepartEntity entity){
        if(entity.getParentId()==null){
            entity.setParentId(0);
        }
        departService.save(entity);
        return R.ok();
    }

    @Log(value = "单位删除",action = 3)
    @RequestMapping("/delete")
    public R delete(@RequestBody JSONObject jsonObject){
        Integer id = jsonObject.getInteger("ids");
        departService.deleteById(id);
        return R.ok();
    }

    @Log(value = "单位修改",action = 2)
    @RequestMapping("/update")
    public R update(@RequestBody SysDepartEntity entity){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("id",entity.getId());
        departService.update(entity,wrapper);
        return R.ok();
    }
}
